﻿using ExamPlat.Repositories;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using ExamPlat.Models;
using ExamPlat.DAL;
using System.Linq;
using ExamPlat.ViewModels;

namespace ExamPlat.Controllers
{
    public class ExamQuesItemController : Controller
    {
        private static IExamQuesItemRepository examQuesItemRepository = new ExamQuesItemRepository();
        private static IExamQuesRepository examQuesRepository = new ExamQuesRepository();
        // GET: ExamQues
        public ActionResult Index(int searchExamID = 0, string searchTitle = "", int searchType = 0)
        {
            if (Request.Cookies["CurUser"] == null)
            {
                return RedirectToAction("Login", "Login");
            }
            HttpCookie curUser = Request.Cookies["CurUser"];
            if (curUser["RoleID"] != "1")
            {
                ViewBag.Msg = "你没有权限查看该页面";
                return View("Error");
            }
            ViewBag.RoleID = curUser["RoleID"];
            IList<ViewExamQuesItem> examQuesItemList = examQuesItemRepository.GetExamQuesItemByCondition(searchExamID, searchTitle, searchType);
            IList<ExamQues> examQuesList = examQuesRepository.GetExamQuesByCondition(string.Empty);
            ViewBag.SearchExamID = searchExamID;
            ViewBag.SearchTitle = searchTitle;
            ViewBag.SearchType = searchType;
            ViewBag.ExamQuesList = examQuesList;
            return View(examQuesItemList);
        }

        public ActionResult Create(int searchExamID = 0, string searchTitle = "", int searchType = 0)
        {
            if (Request.Cookies["CurUser"] == null)
            {
                return RedirectToAction("Login", "Login");
            }
            HttpCookie curUser = Request.Cookies["CurUser"];
            if (curUser["RoleID"] != "1")
            {
                ViewBag.Msg = "你没有权限查看该页面";
                return View("Error");
            }
            ViewBag.RoleID = curUser["RoleID"];
            IList<ExamQues> examQuesList = examQuesRepository.GetExamQuesByCondition(string.Empty);
            ViewBag.SearchExamID = searchExamID;
            ViewBag.SearchTitle = searchTitle;
            ViewBag.SearchType = searchType;
            ViewBag.ExamQuesList = examQuesList;
            return View();
        }

        [HttpPost]
        public ActionResult Create(ExamQuesItem model)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                ViewBag.RoleID = curUser["RoleID"];

                List<ExamQuesItemOption> optionList = new List<ExamQuesItemOption>();
                if (model.ExamQuesItemType == 3)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        string tmpPlaceHolder = "A";
                        ExamQuesItemOption tmpModel = new ExamQuesItemOption();
                        if (i == 1)
                        {
                            tmpPlaceHolder = "B";
                        }
                        tmpModel.ExamQuesItemOptionNum = tmpPlaceHolder;
                        tmpModel.ExamQuesItemOptionTitle = Request.Form["option2" + tmpPlaceHolder];
                        optionList.Add(tmpModel);
                    }
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                    {
                        string tmpPlaceHolder = "A";
                        ExamQuesItemOption tmpModel = new ExamQuesItemOption();
                        if (i == 1)
                        {
                            tmpPlaceHolder = "B";
                        }
                        if (i == 2)
                        {
                            tmpPlaceHolder = "C";
                        }
                        if (i == 3)
                        {
                            tmpPlaceHolder = "D";
                        }
                        if (!string.IsNullOrWhiteSpace(Request.Form["option1" + tmpPlaceHolder]))
                        {
                            tmpModel.ExamQuesItemOptionNum = tmpPlaceHolder;
                            tmpModel.ExamQuesItemOptionTitle = Request.Form["option1" + tmpPlaceHolder];
                            optionList.Add(tmpModel);
                        }
                    }
                }

                if (model.ExamQuesItemAnswer.Contains("、"))
                {
                    string strAnswer = "";
                    IEnumerable<String> tmpStr = model.ExamQuesItemAnswer.Split('、').OrderBy(c => c);
                    foreach (var item in tmpStr)
                    {
                        if (string.IsNullOrWhiteSpace(strAnswer))
                        {
                            strAnswer = item;
                        }
                        else
                        {
                            strAnswer += "、" + item;
                        }
                    }
                    model.ExamQuesItemAnswer = strAnswer;
                }

                int result = examQuesItemRepository.AddExamQuesItem(model, optionList);
                if (result == 1)
                {
                    return RedirectToAction("Index", "ExamQuesItem", new { searchTitle = Request.Form["searchTitle"], searchType = Request.Form["searchType"], searchExamID = Request.Form["searchExamID"] });
                }
                else if (result == -1)
                {
                    ViewBag.Msg = "题号已经存在";
                    return View();
                }
                ViewBag.Msg = "增加失败";
                return View();
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = "增加异常";
                return View();
            }
        }


        public ActionResult Edit(int id, int searchExamID = 0, string searchTitle = "", int searchType = 0)
        {
            if (Request.Cookies["CurUser"] == null)
            {
                return RedirectToAction("Login", "Login");
            }
            HttpCookie curUser = Request.Cookies["CurUser"];
            if (curUser["RoleID"] != "1")
            {
                ViewBag.Msg = "你没有权限查看该页面";
                return View("Error");
            }
            ViewBag.RoleID = curUser["RoleID"];
            IList<ExamQues> examQuesList = examQuesRepository.GetExamQuesByCondition(string.Empty);
            ViewBag.SearchExamID = searchExamID;
            ViewBag.SearchTitle = searchTitle;
            ViewBag.SearchType = searchType;
            ViewBag.ExamQuesList = examQuesList;
            ExamQuesItem model = examQuesItemRepository.GetExamQuesItemByID(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ExamQuesItem model)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                ViewBag.RoleID = curUser["RoleID"];

                List<ExamQuesItemOption> optionList = new List<ExamQuesItemOption>();
                if (model.ExamQuesItemType == 3)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        string tmpPlaceHolder = "A";
                        ExamQuesItemOption tmpModel = new ExamQuesItemOption();
                        if (i == 1)
                        {
                            tmpPlaceHolder = "B";
                        }
                        tmpModel.ExamQuesItemID = model.ID;
                        tmpModel.ExamQuesItemOptionNum = tmpPlaceHolder;
                        tmpModel.ExamQuesItemOptionTitle = Request.Form["option2" + tmpPlaceHolder];
                        optionList.Add(tmpModel);
                    }
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                    {
                        string tmpPlaceHolder = "A";
                        ExamQuesItemOption tmpModel = new ExamQuesItemOption();
                        if (i == 1)
                        {
                            tmpPlaceHolder = "B";
                        }
                        if (i == 2)
                        {
                            tmpPlaceHolder = "C";
                        }
                        if (i == 3)
                        {
                            tmpPlaceHolder = "D";
                        }
                        if (!string.IsNullOrWhiteSpace(Request.Form["option1" + tmpPlaceHolder]))
                        {
                            tmpModel.ExamQuesItemID = model.ID;
                            tmpModel.ExamQuesItemOptionNum = tmpPlaceHolder;
                            tmpModel.ExamQuesItemOptionTitle = Request.Form["option1" + tmpPlaceHolder];
                            optionList.Add(tmpModel);
                        }
                    }
                }

                if (model.ExamQuesItemAnswer.Contains("、"))
                {
                    string strAnswer = "";
                    IEnumerable<String> tmpStr = model.ExamQuesItemAnswer.Split('、').OrderBy(c => c);
                    foreach (var item in tmpStr)
                    {
                        if (string.IsNullOrWhiteSpace(strAnswer))
                        {
                            strAnswer = item;
                        }
                        else
                        {
                            strAnswer += "、" + item;
                        }
                    }
                    model.ExamQuesItemAnswer = strAnswer;
                }

                int result = examQuesItemRepository.UpdateExamQuesItem(model, optionList);
                if (result == 1)
                {
                    return RedirectToAction("Index", "ExamQuesItem", new { searchTitle = Request.Form["searchTitle"], searchType = Request.Form["searchType"], searchExamID = Request.Form["searchExamID"] });
                }
                else if (result == -1)
                {
                    ViewBag.Msg = "题号已经存在";
                    return View();
                }
                ViewBag.Msg = "修改失败";
                return View();
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = "修改异常";
                return View();
            }
        }


        [HttpPost]
        public ActionResult Delete(int id)
        {
            JsonResult result = new JsonResult();
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    result.Data = new { msg = "你没有权限操作" };
                }
                else
                {
                    int tmpResult = examQuesItemRepository.DelExamQuesItem(id);
                    if (tmpResult == 1)
                    {
                        result.Data = new { msg = "删除成功" };
                    }
                    else
                    {
                        result.Data = new { msg = "删除失败" };
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                result.Data = new { msg = "删除异常" };
                return result;
            }
        }
    }
}